package sectorCrack.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;

import sectorCrack.bean.Dat;

public class DataExtracter {

	File file;

	public DataExtracter(String filename) {
		this.file = new File(filename);

	}

	public DataExtracter(File file) {
		this.file = file;
	}

	public ArrayList<Dat> doExtract() throws IOException {
		ArrayList<Dat> resultData = new ArrayList<Dat>();
		FileInputStream fis = new FileInputStream(file);
		byte[] arr = new byte[(int) file.length()];
		fis.read(arr);
		fis.close();
		ByteBuffer bb = ByteBuffer.wrap(arr);

		bb.order(ByteOrder.LITTLE_ENDIAN);

		@SuppressWarnings("unused")
		char c;

		for (int i = 0; i < arr.length; i += 52) {
			Dat dat = new Dat();
			dat.setTimestamp(bb.getInt());
			dat.setElevationMax(bb.getFloat());
			dat.setElevationMin(bb.getFloat());
			dat.setAzimuthMax(bb.getFloat());
			dat.setAzimuthMin(bb.getFloat());
			dat.setSlantRangeMax(bb.getFloat());
			dat.setSlantRangeMin(bb.getFloat());
			dat.setVelocityRaw(bb.getFloat());
			dat.setVelocitySpikeFiltered(bb.getFloat());
			dat.setSnr(bb.getFloat());
			dat.setCfar(bb.getInt());
			dat.setMcfar(bb.getInt());
			dat.setQbit(bb.getInt());
			resultData.add(dat);
		}
		return resultData;
	}

}
